// pages/template/entrepreneurship/entrepreneurship.js
var GlobalData = require('../../../utils/GlobalData.js');
var dataManager = require('../../../utils/dataManager.js');
var Util = require('../../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [
      '/images/10.png',
      '/images/20.png',
      '/images/30.png'
    ],
    open: false,
    mark: 0,
    newmark: 0,
    istoright: true,
    indicatorDots: false,
    autoplay: true,
    interval: 2000,
    duration: 1000,
    promotebalance: 0,
    isposter: 0,
    longurl: "",
    posterArray: [],
    hidden: true,
    isShowRecharge: false,
    kakuImg: "kaku2",
    headImg: '/images/cyj_bg.png'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var img = ""
    // if (app.getConfig().isShowRecharge) {
    //   console.log("app.GlobalData.first", app.GlobalData.first);
    //   this.data.kakuImg = "kaku2";
    //   this.data.imgUrls = [
    //     '/images/10.png',
    //     '/images/20.png',
    //     '/images/30.png'
    //   ];
    //   this.data.headImg = '/images/cyj_bg.png';
    //   app.GlobalData.first = app.GlobalData.first - 1;
    // }
    // else {
    //   console.log("app.GlobalData", app.GlobalData.first);
    //   this.data.kakuImg = "haibao2";
    //   this.data.imgUrls = [
    //     '/images/10.png',
    //     '/images/20.png',
    //     '/images/30.png'
    //   ];
    //   this.data.headImg = '/images/haibaohead.png';
    //   app.GlobalData.first = app.GlobalData.first - 1;
    // }


    this.setData({
      headImg: this.data.headImg,
      imgUrls: this.data.imgUrls,
      kakuImg: this.data.kakuImg,
      isShowRecharge: app.getConfig().isShowRecharge
    })

    this.setData({
      hidden: true,
      promotebalance: GlobalData.homeData.promotebalance ? GlobalData.homeData.promotebalance : 0,
      isposter: GlobalData.homeData.isposter ? GlobalData.homeData.isposter : 0,
    })

    if (GlobalData.homeData.isposter && GlobalData.posterArr.length) {
      this.setData({
        imgUrls: GlobalData.posterArr
      })

    } else {
      var urla = "https://shopapi.queyouquan.cn/prot/ShowAgent.html?sysno=" + GlobalData.homeData.sysno
      this.setData({
        longurl: urla
      })
      //获取长链
      // wx.request({
      //   url: app.getConfig().url,
      //   header: {
      //     "Content-Type": "application/x-www-form-urlencoded"
      //   },
      //   method: "POST",
      //   data: Util.json2Form({ method: "Getshorturl", longurl: url }),
      //   complete: function (res) {
      //     console.log(res.data.longurl);
      //   }
      // })
      /**获取海报图片 */
      this.onShipMethod();
    }



  },

  /**点击生成海报 */
  getShip: function () {
    var that = this;
    if (GlobalData.posterArr.length) {
      that.setData({
        imgUrls: GlobalData.posterArr
      })
    } else {
      /**获取海报图片 */
      that.onShipMethod();
    }
    // 需要预览的图片http链接列表  
    // console.log(that.data.posterArray,"需要预览的图片http链接列表")
    wx.previewImage({
      urls: GlobalData.posterArr,
      success: function (res) {
      }
    })
  },
  /**获取海报图片 */
  onShipMethod: function () {
    // GlobalData.homeData.isposter = 0
    try {
      var check = 0;
      if (app.getConfig().isShowRecharge) {
        check = 1;
      } else
        check = 0;
      var dataArg = { "method": "posterbackground", "url": this.data.longurl, "sysno": GlobalData.homeData.sysno, "isposter": GlobalData.homeData.isposter, "isaudit": check }
      dataManager.OnRequestPost(dataArg, this.onProcessShip)
    }
    catch (error) {
      console.log("生成海报失败")
    }
  },
  onProcessShip: function (data) {
    var posterArr = [];
    for (let i = 0; i < data.length; i++) {
      posterArr[i] = data[i]["url"];
    }
    GlobalData.posterArr = posterArr;
    this.setData({
      posterArray: GlobalData.posterArr
    })
    if (GlobalData.posterArr.length && GlobalData.homeData.isposter) {
      this.setData({
        imgUrls: GlobalData.posterArr
      })
    }
    // console.log(this.data.imgUrls);
  },
  /**查看收益明细 */
  goEarningDetail: function () {
    this.setData({
      hidden: false
    })
    dataManager.OnRequestPost({ method: "getpromoterebatelist", "token": GlobalData.token, "pageindex": GlobalData.EarningDetailother["index"], "pagesize": GlobalData.EarningDetailother["size"] }, this.onProcessEarning, false, this.onError);
  },
  onError: function () {
    this.setData({
      hidden: true
    })
  },
  onProcessEarning: function (data) {
    for (let i = 0; i < data.length; i++) {
      GlobalData.EarningDetail[i] = GlobalData.clone(data[i]);
    }
    this.setData({
      hidden: true
    })
    dataManager.UseNavigateTo("/pages/component/homes/earningdetail/earningdetail", false);

  },


  showTxt: function (e) {
    var str = ""
    if (GlobalData.homeData.businessname)
      str = GlobalData.homeData.businessname
    var msg = "添加商务微信" + GlobalData.homeData.businessname + "，即可开通推广功能"
    wx.showModal({
      title: "提示",
      content: msg,
      showCancel: true,
      confirmText: "确认复制",
      success: function (res) {
        if (res.confirm) {
          wx.setClipboardData({
            data: str,
            success() {
              wx.getClipboardData({
                success(res) {
                  // app.show("已复制成功。")
                }
              })
            },
            fail(res) {
              console.error(res)
            }
          })
        }
      }
    })
  },
  /**前往推广 */
  goPromotion: function () {
    if (!GlobalData.homeData.isopendistribution) {
      app.show("该地区暂未开启推广功能");
      return;
    }
    else if (GlobalData.homeData.isopendistribution && (GlobalData.homeData.isdefaultlevels && !GlobalData.homeData.isdefaultshare)) {
      // this.showTxt("提示", "添加商务微信" + GlobalData.homeData.businessname + "，即可开通推广功能", "确认复制微信号");
      this.showTxt();
      return;
    }
    else {
      // wx.redirectTo({
      //   url: '../../promotions/promotion/promotion'
      // })
      this.setData({
        hidden: false
      })
      dataManager.UseNavigateTo('/pages/component/promotions/promotion/promotion', true);
    }

  },
  /**前往主页 */
  goHome: function () {
    // wx.redirectTo({
    //   url: '../../homes/home/home'
    // })
    this.setData({
      hidden: false
    })
    dataManager.UseNavigateTo("/pages/component/homes/home/home", true);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})